﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Web.Enhancements.Rest;
using Web.Enhancements.Rest.Filters;

namespace TestWeb25.Components
{
    [System.ComponentModel.Description("Class provides generic services for testing purposes.")]
    public class InfoPreviewHandler : IWebRestService
    {
        [WebRestMethodAttribute()]
        [DefaultHttpGet()]
        [System.ComponentModel.Description("Method returns an extra information based on specified info id.")]
        public string GetExtraInfo(int infoId)
        {
            return string.Format("This is return from InfoPreviewHandler.GetExtraInfo method. You requested info for infoId = " + infoId.ToString());
        }

        [WebRestMethodAttribute(false, System.Web.SessionState.SessionStateBehavior.Required)]
        [WebMethodFilter(HttpMethod = HttpMethodRequirement.GetOrPost)]
        [RequiresSslFilter(false)]
        public string GetSessionData(string sessionKey)
        {
            if (System.Web.HttpContext.Current.Session != null)
            {
                if (System.Web.HttpContext.Current.Session[sessionKey] != null)
                    return string.Format("{0}. Time stamp: {1}.", System.Web.HttpContext.Current.Session[sessionKey].ToString(), DateTime.Now.ToString());
                else
                    return "The Session is present, but value is null. If InProc session is used, this can happen if application has restarted.";
            }
            return "The Session is null.";
        }

        //no WebMethod attribute test
        public string GetUserData(string sessionKey)
        {
            //System.Web.UI.DataVisualization.Charting.Chart chart = new System.Web.UI.DataVisualization.Charting.Chart();
            //chart.ImageStorageMode = System.Web.UI.DataVisualization.Charting.ImageStorageMode.UseImageLocation;
            //chart.ChartAreas.Add("temp");
            //chart.Series.Add("line");
            return "The method should not been go through as it has no WebRestMethodAttribute.";
        }

        [WebRestMethodAttribute(false)]
        public string GetGreeting(string userName)
        {
            return Web.Enhancements.Rest.Templates.HtmlTemplateFactory.RenderTemplate<string>("TestControl.ascx", userName);
        }

        [WebRestMethodAttribute(false)]
        public Web.Enhancements.Rest.Templates.TemplatedResult<string> GetGreeting2(string userName)
        {
            Web.Enhancements.Rest.Templates.TemplatedResult<string> result = new Web.Enhancements.Rest.Templates.TemplatedResult<string>("TestControl.ascx", userName);
            return result;
        }

        [WebRestMethodAttribute(ContentDisposition = "filename=HelpFile.pdf")]
        [WebRestCache(Duration = 20, VaryByParam = "All")]
        public Web.Enhancements.Rest.Templates.StaticFileResult GetStaticFile(string documentName)
        {
            Web.Enhancements.Rest.Templates.StaticFileResult result = new Web.Enhancements.Rest.Templates.StaticFileResult()
                {
                    FilePath = string.Format("Content/{0}.pdf", documentName),
                    ContentType = "application/pdf"
                };
            return result;
        }

        [WebRestMethodAttribute()]
        [HttpBodyParameter(ParameterName = "incomingData")]
        public string ResendData(string incomingData)
        {
            return incomingData;
        }

        [WebRestMethodAttribute()]
        [HttpBodyParameter(ParameterName = "incomingData")]
        public string ResendDataWithExtra(int userId, string incomingData, string extraData)
        {
            if (userId <= 0)
                throw new Exception("UserId is incorrect");
            if (extraData.Length == 0)
                throw new Exception("extraData is incorrect");

            return incomingData;
        }

        [WebRestMethodAttribute()]
        [HttpBodyParameter(ParameterName = "incomingData", ConversionHint = ParameterConversionHint.Json)]
        public string ConvertResendData(Employee incomingData)
        {
            return incomingData.FirstName;
        }

        [WebRestMethodAttribute()]
        [HttpBodyParameter(ParameterName = "incomingData")]
        public int AcceptBinaryData(byte[] incomingData)
        {
            return incomingData.Length;
        }
    }
}
